package com.jiaofamily.localad.sdk.adapters;

import android.graphics.Color;
import android.util.Log;

import com.jiaofamily.localad.sdk.LocalAdLayout;
import com.jiaofamily.localad.sdk.obj.Extra;
import com.jiaofamily.localad.sdk.obj.Ration;
import com.jiaofamily.localad.sdk.util.LocalAdUtil;
import com.wiyun.ad.AdView;
import com.wiyun.ad.AdView.AdListener;

public class WiyunAdapter extends LocalAdAdapter implements AdListener{
	private static final String TAG=WiyunAdapter.class.getSimpleName();
	
	private AdView adView=null;
	
	public WiyunAdapter(LocalAdLayout layout, Ration ration) {
		super(layout, ration);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void handle() {
		// TODO Auto-generated method stub
		Log.d(TAG,"Enter into Wiyun");
		LocalAdLayout layout=this.layout.get();
		if(layout==null){
			return;
		}
		Extra extra=layout.extra;
		int bgColor=Color.rgb(extra.bgRed, extra.bgGreen, extra.bgBlue);
		int fgColor=Color.rgb(extra.fgRed, extra.fgGreen, extra.fgBlue);
		
		adView=new AdView(layout.getContext());
		adView.setBackgroundColor(bgColor);
		adView.setTextColor(fgColor);
		adView.setResId(this.ration.key);
		adView.setTestMode(false);
		adView.setListener(this);
		adView.requestAd();	
		layout.manager.resetRollover();

		layout.handler.post(new LocalAdLayout.ViewAdRunnable(layout, adView));
		layout.rotateThreadedDelayed();
	}

	public void onAdClicked() {
		// TODO Auto-generated method stub
		Log.d(TAG,"Wiyun onAdClicked");
		Count.countClick(this.layout.get().getContext(),this.layout.get().activeRation);
	}

	public void onAdLoadFailed() {
		// TODO Auto-generated method stub
		Log.d(TAG,"Wiyun onAdLoadFailed");
		if(this.adView!=null){
			this.adView.setListener(null);
		}
		
		LocalAdLayout layout=this.layout.get();
		if(layout==null){
			return;
		}
		//reduce weight
		if(LocalAdLayout.isDynamicAdjustWeight) {
			layout.manager.subWeight(LocalAdUtil.NETWORK_TYPE_WIYUN);
		}
		
		layout.manager.resetRollover_pri();
		layout.rollover_pri();
	}

	public void onAdLoaded() {
		// TODO Auto-generated method stub
		Log.d(TAG,"Wiyun onAdLoaded");
		Count.countShow(this.layout.get().getContext(),this.layout.get().activeRation);
//		LocalAdLayout layout=this.layout.get();
//		if(layout==null){
//			return;
//		}
		
//		layout.manager.resetRollover();
//		if(this.adView!=null){
//			layout.handler.post(new LocalAdLayout.ViewAdRunnable(layout, this.adView));
//		}
//		layout.rotateThreadedDelayed();
	}

	public void onExitButtonClicked() {
		// TODO Auto-generated method stub
		
	}

}
